package cn.demoncat.util.pay.cmb.entity;

import cn.demoncat.util.lang.IDUtil;
import cn.demoncat.util.lang.JsonUtil;
import cn.demoncat.util.lang.LocalDateUtil;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 招行请求参数
 *
 * @author 延晓磊
 * @since 2024年10月31日
 */
@Data
public class CmbParam {

    private Request request;
    private Signature signature;

    @Data
    public static class Request{

        private Head head;
        private Object body;
    }

    @Data
    public static class Head{

        // 接口名称
        private String funcode;
        // 用户ID
        private String userid;
        // 请求ID：yyyyMMddHHmmssSSS + 随机数
        private String reqid;
    }

    @Data
    public static class Signature{

        // 时间：yyyyMMddHHmmss
        private String sigtim;
        // 预填充
        private String sigdat;
    }

    /**
     * 构建请求参数
     *
     * @param funcode
     * @param uid
     * @param body
     * @return
     */
    public static String build(String funcode, String uid, Object body){
        LocalDateTime now = LocalDateTime.now();
        CmbParam param = new CmbParam();
        param.request = new CmbParam.Request();
        param.request.head = new CmbParam.Head();
        param.request.head.funcode = funcode;
        param.request.head.userid = uid;
        param.request.head.reqid = LocalDateUtil.format(now, DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + IDUtil.getTime();
        param.request.body = body;
        param.signature = new Signature();
        param.signature.sigtim = LocalDateUtil.format(now, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        param.signature.sigdat = "__signature_sigdat__";
        return JsonUtil.toString(param);
    }

}
